//
//  LZManagePictureCollectionViewCell.m
//  laziz_Merchant
//
//  Created by XMac on 17/3/9.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZManagePictureCollectionViewCell.h"
#import "UIGestureRecognizer+BlocksKit.h"
#import "extobjc.h"
#import "UIImageView+WebCache.h"


NSString *const LZManagePictureCollectionViewCellEditNotification = @"LZManagePictureCollectionViewCellEditNotification";

@interface LZManagePictureCollectionViewCell ()

@property (weak, nonatomic) IBOutlet UIImageView *contentImageView;

@property (weak, nonatomic) IBOutlet UIImageView *selectImageView;



@end

@implementation LZManagePictureCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    _selectImageView.hidden = YES;
    
    [_contentImageView setUserInteractionEnabled:YES];
    
    @weakify(self)
    UITapGestureRecognizer *tap = [UITapGestureRecognizer bk_recognizerWithHandler:^(UIGestureRecognizer *sender, UIGestureRecognizerState state, CGPoint location) {
        @strongify(self)
        
        if (self.isEditing) {
            self.isSelect = !self.isSelect;
            
            if (self.operationSelectedBlock) {
                self.operationSelectedBlock(self.isSelect ?PICTURE_OPERATION_ADD:PICTURE_OPERATION_DELETE,self.photoId,self);
            }
            
        }else{
            [self showPicture];
        }
    }];
    
    [_contentImageView addGestureRecognizer:tap];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveEditNotification:) name:LZManagePictureCollectionViewCellEditNotification object:nil];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


/**
 接收关于是否进入编辑状态的通知
 */
-(void)receiveEditNotification:(NSNotification *)notification{
    
    NSNumber *isEdit = notification.object;
    
    self.operationSelectedBlock = notification.userInfo[@"OPERATION_BLOCK"];
    
    self.isEditing = isEdit.boolValue;
}


-(void)setIsEditing:(BOOL)isEditing{
    
    _isEditing = isEditing;
    
    
    //退出编辑状态
    if (!_isEditing) {
        _selectImageView.hidden = YES;
        
        _isSelect = NO;
    }
}

-(void)setIsSelect:(BOOL)isSelect{
    
    if (_isEditing) {
        _isSelect = isSelect;
        
        _selectImageView.hidden = !_isSelect;
    }
}


-(void)setImageUrl:(NSString *)imageUrl{
    
    _imageUrl = imageUrl;
    
    [self.contentImageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:[UIImage imageNamed:@"lz_other_placehoiderImage"]];
}


-(void)showPicture{

    if (self.pictureTapBlock) {
        self.pictureTapBlock(self.index,self.contentImageView,self.imageUrl);
    }
}


@end
